import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/index.vue')
    },
    {
        path: '/admin',
        name: 'AdminLayout',
        component: () => import('../views/admin/Layout.vue'),
        meta: { requiresAuth: true, userType: '管理员' },
        children: [
            {
                path: '',
                name: 'AdminDashboard',
                component: () => import('../views/admin/Dashboard.vue')
            },
            // 人员管理模块路由
            {
                path: 'personnel',
                name: 'PersonnelManagement',
                component: () => import('../views/admin/personnel/index.vue'),
                meta: { title: '人员管理' }
            },
            {
                path: 'personnel/company',
                name: 'CompanyManagement',
                component: () => import('../views/admin/personnel/CompanyList.vue'),
                meta: { title: '公司管理' }
            },
            {
                path: 'personnel/company-employee',
                name: 'CompanyEmployeeManagement',
                component: () => import('../views/admin/personnel/CompanyEmployeeList.vue'),
                meta: { title: '企业员工管理' }
            },
            {
                path: 'personnel/park-employee',
                name: 'ParkEmployeeManagement',
                component: () => import('../views/admin/personnel/ParkEmployeeList.vue'),
                meta: { title: '物业员工管理' }
            },
            {
                path: 'personnel/visitor',
                name: 'VisitorManagement',
                component: () => import('../views/admin/personnel/VisitorList.vue'),
                meta: { title: '访客管理' }
            },
            // 资产管理模块路由
            {
                path: 'assets',
                redirect: '/admin/assets/BuildingList'
            },
            {
                path: 'assets/BuildingList',
                name: 'BuildingManagement',
                component: () => import('../views/admin/assets/BuildingList.vue'),
                meta: { title: '空间管理' }
            },
            {
                path: 'assets/BuildingDetail/:id',
                name: 'BuildingDetail',
                component: () => import('../views/admin/assets/BuildingDetail.vue'),
                meta: { title: '楼宇详情' }
            },
            {
                path: 'assets/FloorDetail/:id',
                name: 'FloorDetail',
                component: () => import('../views/admin/assets/FloorDetail.vue'),
                meta: { title: '楼层详情' }
            },
            {
                path: 'assets/RoomDetail/:id',
                name: 'RoomDetail',
                component: () => import('../views/admin/assets/RoomDetail.vue'),
                meta: { title: '房间详情' }
            },
            {
                path: 'assets/EquipmentList',
                name: 'EquipmentManagement',
                component: () => import('../views/admin/assets/EquipmentList.vue'),
                meta: { title: '设备管理' }
            },
            {
                path: 'assets/EquipmentDetail/:id',
                name: 'EquipmentDetail',
                component: () => import('../views/admin/assets/EquipmentDetail.vue'),
                meta: { title: '设备详情' }
            },
            {
                path: 'assets/VehicleList',
                name: 'VehicleManagement',
                component: () => import('../views/admin/assets/VehicleList.vue'),
                meta: { title: '车辆管理' }
            },
            {
                path: 'assets/VehicleDetail/:id',
                name: 'VehicleDetail',
                component: () => import('../views/admin/assets/VehicleDetail.vue'),
                meta: { title: '车辆详情' }
            },
            // 服务管理模块路由
            {
                path: 'services',
                redirect: '/admin/services/repair'
            },
            {
                path: 'services/:serviceType',
                name: 'ServiceManagement',
                component: () => import('../views/admin/services/index.vue'),
                props: true, // Pass route params as props to the component
                meta: { title: '服务管理' }
            },
            {
                path: 'services/temporary-parking',
                name: 'TemporaryParking',
                component: () => import('../views/admin/services/TemporaryParking.vue'),
                meta: { title: '临时停车服务' }
            },
            // 财务管理模块路由
            {
                path: 'finance/contracts',
                name: 'ContractList',
                component: () => import('../views/admin/finance/ContractList.vue'),
                meta: { title: '合同列表' }
            },
            {
                path: 'finance/contracts/:id',
                name: 'ContractDetail',
                component: () => import('../views/admin/finance/ContractDetail.vue'),
                meta: { title: '合同详情' }
            },
            {
                path: 'finance/bills',
                name: 'BillList',
                component: () => import('../views/admin/finance/BillList.vue'),
                meta: { title: '账单列表' }
            },
            {
                path: 'finance/bills/:id',
                name: 'BillDetail',
                component: () => import('../views/admin/finance/BillDetail.vue'),
                meta: { title: '账单详情' }
            },
            // 通知管理
            {
                path: 'notifications',
                redirect: '/admin/notifications/list'
            },
            {
                path: 'notifications/list',
                name: 'NotificationManagement',
                component: () => import('../views/admin/notifications/NotificationList.vue'),
                meta: { title: '通知管理' }
            },
            {
                path: 'notifications/drafts',
                name: 'NotificationDrafts',
                component: () => import('../views/admin/notifications/NotificationDraft.vue'),
                meta: { title: '通知草稿箱' }
            },
            {
                path: 'notifications/publish',
                name: 'NotificationPublish',
                component: () => import('../views/admin/notifications/NotificationPublish.vue'),
                meta: { title: '发布新通知' }
            },
            {
                path: 'notifications/feedback',
                name: 'FeedbackManagement',
                component: () => import('../views/admin/notifications/FeedbackList.vue'),
                meta: { title: '问题反馈' }
            },
            // 数据大屏
            {
                path: 'analytics/screen',
                name: 'DataScreen',
                component: () => import('../views/admin/analytics/DataScreen.vue'),
                meta: { title: '数据大屏' }
            }
        ]
    },
    {
        path: '/data-screen',
        name: 'DataScreenFullscreen',
        component: () => import('../views/admin/analytics/DataScreen.vue'),
        meta: { requiresAuth: true, userType: '管理员', title: '数据大屏 - 全屏' }
    },
    {
        path: '/staff',
        name: 'StaffLayout',
        component: () => import('../views/staff/Layout.vue'),
        meta: { requiresAuth: true, userType: '物业员工' },
        children: [
            {
                path: '',
                name: 'StaffDashboard',
                component: null,
                beforeEnter: (to, from, next) => {
                    // 根据员工类型重定向到对应的工作台
                    const employeeType = localStorage.getItem('employeeType')
                    if (employeeType === '送水工') {
                        next('/staff/water-dashboard')
                    } else if (employeeType === '保洁工') {
                        next('/staff/cleaning-dashboard')
                    } else if (employeeType === '维修工') {
                        next('/staff/repair-dashboard')
                    } else {
                        // 未知角色时，重定向到个人中心，避免空白页
                        next('/staff/profile')
                    }
                }
            },
            {
                path: 'task-hall',
                name: 'TaskHall',
                component: null,
                meta: { title: '接单大厅' },
                beforeEnter: (to, from, next) => {
                    // 根据员工类型重定向到对应的接单大厅
                    const employeeType = localStorage.getItem('employeeType')
                    if (employeeType === '送水工') {
                        next('/staff/water-task-hall')
                    } else if (employeeType === '保洁工') {
                        next('/staff/cleaning-task-hall')
                    } else if (employeeType === '维修工') {
                        next('/staff/repair-task-hall')
                    } else {
                        // 未知角色时，重定向到个人中心，避免空白页
                        next('/staff/profile')
                    }
                }
            },
            // 维修工相关路由
            // {
            //     path: 'repair-tasks',
            //     name: 'RepairTasks',
            //     component: () => import('../views/staff/repair/RepairTasks.vue'),
            //     meta: { title: '维修工单' }
            // },
            {
                path: 'repair-order/:id',
                name: 'RepairOrderDetail',
                component: () => import('../views/staff/repair/RepairOrderDetail.vue'),
                meta: { title: '维修工单详情' }
            },
            {
                path: 'repair-dashboard',
                name: 'RepairDashboard',
                component: () => import('../views/staff/repair/RepairDashboard.vue'),
                meta: { title: '维修工作台' }
            },
            {
                path: 'repair-task-hall',
                name: 'RepairTaskHall',
                component: () => import('../views/staff/repair/RepairTaskHall.vue'),
                meta: { title: '维修接单大厅' }
            },
            // 保洁工相关路由
            // {
            //     path: 'cleaning-tasks',
            //     name: 'CleaningTasks',
            //     component: () => import('../views/staff/cleaning/CleaningTasks.vue'),
            //     meta: { title: '保洁工单' }
            // },
            {
                path: 'cleaning-order/:id',
                name: 'CleaningOrderDetail',
                component: () => import('../views/staff/cleaning/CleaningOrderDetail.vue'),
                meta: { title: '保洁工单详情' }
            },
            {
                path: 'cleaning-dashboard',
                name: 'CleaningDashboard',
                component: () => import('../views/staff/cleaning/CleaningDashboard.vue'),
                meta: { title: '保洁工作台' }
            },
            {
                path: 'cleaning-task-hall',
                name: 'CleaningTaskHall',
                component: () => import('../views/staff/cleaning/CleaningTaskHall.vue'),
                meta: { title: '保洁接单大厅' }
            },
            // 送水工相关路由
            {
                path: 'water-order/:id',
                name: 'WaterOrderDetail',
                component: () => import('../views/staff/water/WaterOrderDetail.vue'),
                meta: { title: '送水工单详情' }
            },
            {
                path: 'water-dashboard',
                name: 'WaterDashboard',
                component: () => import('../views/staff/water/WaterDashboard.vue'),
                meta: { title: '送水工作台' }
            },
            {
                path: 'water-task-hall',
                name: 'WaterTaskHall',
                component: () => import('../views/staff/water/WaterTaskHall.vue'),
                meta: { title: '送水接单大厅' }
            },
            // 公共组件路由
            {
                path: 'profile',
                name: 'StaffProfile',
                component: () => import('../views/staff/Profile.vue'),
                meta: { title: '个人中心' }
            },
            {
                path: 'history',
                name: 'StaffHistory',
                component: null,
                meta: { title: '历史记录' },
                beforeEnter: (to, from, next) => {
                    // 根据员工类型重定向到对应的历史记录
                    const employeeType = localStorage.getItem('employeeType')
                    if (employeeType === '送水工') {
                        next('/staff/water-history')
                    } else if (employeeType === '保洁工') {
                        next('/staff/cleaning-history')
                    } else if (employeeType === '维修工') {
                        next('/staff/repair-history')
                    } else {
                        // 未知角色时，重定向到个人中心，避免空白页
                        next('/staff/profile')
                    }
                }
            },
            // 送水工历史记录
            {
                path: 'water-history',
                name: 'WaterHistory',
                component: () => import('../views/staff/water/WaterHistory.vue'),
                meta: { title: '送水历史记录' }
            },
            // 保洁工历史记录
            {
                path: 'cleaning-history',
                name: 'CleaningHistory',
                component: () => import('../views/staff/cleaning/CleaningHistory.vue'),
                meta: { title: '保洁历史记录' }
            },
            // 维修工历史记录
            {
                path: 'repair-history',
                name: 'RepairHistory',
                component: () => import('../views/staff/repair/RepairHistory.vue'),
                meta: { title: '维修历史记录' }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const userType = localStorage.getItem('userType')

    // 如果用户已登录
    if (token) {
        // 如果已登录用户访问登录页，则重定向到其主页
        if (to.path === '/login') {
            if (userType === '管理员') {
                next({ path: '/admin' })
            } else {
                // 根据员工类型重定向到对应的工作台
                const employeeType = localStorage.getItem('employeeType')
                if (employeeType === '送水工') {
                    next({ path: '/staff/water-dashboard' })
                } else if (employeeType === '保洁工') {
                    next({ path: '/staff/cleaning-dashboard' })
                } else if (employeeType === '维修工') {
                    next({ path: '/staff/repair-dashboard' })
                } else {
                    next({ path: '/staff' })
                }
            }
        } else {
            // 检查路由是否需要特定的用户类型
            if (to.matched.some(record => record.meta.userType) && to.meta.userType !== userType) {
                // 用户类型不匹配，重定向到各自的主页
                if (userType === '管理员') {
                    next({ path: '/admin' })
                } else {
                    // 根据员工类型重定向到对应的工作台
                    const employeeType = localStorage.getItem('employeeType')
                    if (employeeType === '送水工') {
                        next({ path: '/staff/water-dashboard' })
                    } else if (employeeType === '保洁工') {
                        next({ path: '/staff/cleaning-dashboard' })
                    } else if (employeeType === '维修工') {
                        next({ path: '/staff/repair-dashboard' })
                    } else {
                        next({ path: '/staff' })
                    }
                }
            } else {
                // 用户类型匹配或路由不需要特定用户类型，允许访问
                next()
            }
        }
    } else {
        // 用户未登录
        // 如果目标路由需要认证，则重定向到登录页
        if (to.matched.some(record => record.meta.requiresAuth)) {
            next({ path: '/login' })
        } else {
            // 否则，允许访问
            next()
        }
    }
})

export default router 